package com.dragon.modules.yuegou.client.wechat.model;

import com.dragon.modules.yuegou.properties.AggrepayProperties;
import com.dragon.modules.yuegou.utils.MD5;
import com.dragon.modules.yuegou.utils.Tools;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
/**
 * 微信订单查询model
 * @author 程泽
 */
public class WXOrderQueryVO {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    // 必填
    private String appid;// 服务商商户的APPID
    private String mch_id;// 微信支付分配的商户号
    private String sub_mch_id; //特约商户的商户号
    private String nonce_str;//随机字符串，不长于32位
    private String sign;// 签名，详见签名生成算法===>算出来的
    //二选一
    private String transaction_id;//微信的订单号，优先使用
    private String out_trade_no;//商户系统内部订单号，要求32个字符内，只能是数字、大小写字母_-|*@ ，且在同一个商户号下唯一。
    // 非必填
    private String sub_appid; //微信分配的子商户公众账号ID

    public WXOrderQueryVO(AggrepayProperties aggrepayProperties) {
        super();
        this.appid = aggrepayProperties.getWxappid();
        this.mch_id = aggrepayProperties.getMchid();
        this.nonce_str = Tools.createRandom(false, 15);
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getSub_mch_id() {
        return sub_mch_id;
    }

    public void setSub_mch_id(String sub_mch_id) {
        this.sub_mch_id = sub_mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getTransaction_id() {
        return transaction_id;
    }

    public void setTransaction_id(String transaction_id) {
        this.transaction_id = transaction_id;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }

    public String getSub_appid() {
        return sub_appid;
    }

    public void setSub_appid(String sub_appid) {
        this.sub_appid = sub_appid;
    }

    public void wxsign(AggrepayProperties aggrepayProperties) {
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        parameters.put("appid", this.appid);

        parameters.put("mch_id", this.mch_id);
        parameters.put("sub_mch_id", this.sub_mch_id);
        parameters.put("nonce_str", this.nonce_str);
        if(!StringUtils.isEmpty(this.transaction_id)){
            parameters.put("transaction_id", this.transaction_id);
        }else {
            parameters.put("out_trade_no", this.out_trade_no);
        }

        String createSign = createSign(parameters,aggrepayProperties.getKey());
        logger.debug("SIGN:" + createSign);
        this.sign = createSign;
    }

    @SuppressWarnings("rawtypes")
    public static String createSign(SortedMap<Object, Object> parameters,
                                    String key) {

        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        String sign = MD5.wxsign(sb.toString(), key, "utf-8").toUpperCase();
        return sign;
    }
}

